//
//  JKRFeedAuthorView.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/10.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedAuthorView.h"

@interface JKRFeedAuthorView ()

@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) YYLabel *nameLabel;
@property (nonatomic, strong) UIImageView *baojiLevelImageView;
@property (nonatomic, strong) YYLabel *timeLabel;
@property (nonatomic, strong) YYLabel *priceLabel;
@property (nonatomic, strong) YYLabel *gameLabel;

@end

@implementation JKRFeedAuthorView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    @weakify(self);
    self.backgroundColor = [UIColor whiteColor];
    _headerImageView = [UIImageView new];
    _headerImageView.userInteractionEnabled = YES;
    [_headerImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerImageViewClick)]];
    [self addSubview:_headerImageView];
    _nameLabel = [self _creatYYLabel];
    _nameLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weak_self highLightClickWithText:text range:range];
    };
    [self addSubview:_nameLabel];
    _baojiLevelImageView = [UIImageView new];
    [self addSubview:_baojiLevelImageView];
    _timeLabel = [self _creatYYLabel];
    [self addSubview:_timeLabel];
    _priceLabel = [self _creatYYLabel];
    [self addSubview:_priceLabel];
    _gameLabel = [self _creatYYLabel];
    [self addSubview:_gameLabel];
    return self;
}

- (void)setLayout:(JKRFeedAuthorLayout *)layout {
    _layout = layout;

    self.frame = layout.authorFrame;
    self.headerImageView.frame = layout.headerImageFrame;
    [self.headerImageView setImageWithURL:layout.headerImageURL placeholder:nil options:kNilOptions manager:[JKRFeedDetailHelper circleHeaderImageManager] progress:nil transform:nil completion:nil];
    self.nameLabel.frame = layout.nameLabelFrame;
    self.nameLabel.textLayout = layout.nameLabelLayout;
    self.baojiLevelImageView.frame = layout.baoJiImageViewFrame;
    self.baojiLevelImageView.image = [JKRFeedDetailHelper imageNamed:layout.baojiImageName];
    self.priceLabel.frame = layout.priceLabelFrame;
    self.priceLabel.textLayout = layout.priceLabelLayout;
    self.timeLabel.frame = layout.timeLabelFrame;
    self.timeLabel.textLayout = layout.timeLabelLayout;
    self.gameLabel.frame = layout.gameLabelFrame;
    self.gameLabel.textLayout = layout.gameLabelLayout;
}

- (void)headerImageViewClick {
    YYTextHighlight *highlight = [self.nameLabel.textLayout.text attribute:YYTextHighlightAttributeName atIndex:0];
    NSDictionary *info = highlight.userInfo;
    if (info && info.count && info[kFeedTopClickUser]) {
        [self.topView.delegate topViewDidClickUserWithUserID:info[kFeedTopClickUser]];
    }
}

- (void)highLightClickWithText:(NSAttributedString *)text range:(NSRange)range {
    YYTextHighlight *highlight = [text attribute:YYTextHighlightAttributeName atIndex:range.location];
    NSDictionary *info = highlight.userInfo;
    if (info.count == 0) {
        return;
    }
    if (info[kFeedTopClickUser]) {
        [self.topView.delegate topViewDidClickUserWithUserID:info[kFeedTopClickUser]];
        return;
    }
}

- (YYLabel *)_creatYYLabel {
    YYLabel *label = [YYLabel new];
    label.ignoreCommonProperties = YES;
    label.fadeOnAsynchronouslyDisplay = YES;
    return label;
}

@end
